﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Data.SqlClient;
using System.Linq;

using AtletIS.DomainModel.DTOs;
using AtletIS.DomainModel.Entities.Main;
using AtletIS.Repository.UnitOfWork;
using AtletIS.Repository.Repositories;
using AtletIS.Service.Base;

namespace AtletIS.Service.Services
{
    public class ChipResultsService : ServiceBase
    {
        #region Constructor

        public ChipResultsService()
        {
            this._unitOfWork = new UnitOfWork();
        }

        public ChipResultsService(IUnitOfWork unitOfWork)
        {
            this._unitOfWork = unitOfWork;
        }

        #endregion

        #region Check point

        /// <summary>
        ///  Chipové výsledky pre check point
        /// </summary>
        public List<ChipResultsDto> GetByCheckPoint(int checkPointId)
        {
            CheckPoint checkPoint = new CheckPointsService(this._unitOfWork).GetById(checkPointId);
            
            return this.GetByCheckPoint(checkPoint);
        }

        /// <summary>
        ///  Chipové výsledky pre check point
        /// </summary>
        public List<ChipResultsDto> GetByCheckPoint(CheckPoint checkPoint)
        {
            List<ChipResultsDto> chipResults = this._unitOfWork
                .Repository<ChipData>()
                .GetChipDataForCheckPoint(checkPoint);
            
            return chipResults;
        }

        /// <summary>
        ///  Chipové výsledky pre check point (procedúrou pre kontrolu)
        /// </summary>
        public List<ChipResultsDto> GetByCheckPointId(int checkPointId)
        {
            var checkPointIdParameter = new SqlParameter("@CheckPointId", checkPointId);

            return this._unitOfWork
                .ExecuteQuery<ChipResultsDto>("EXEC GetChipDataByCheckPointId @CheckPointId", checkPointIdParameter)
                .ToList();
        }

        #endregion

        /// <summary>
        ///  Vráti chip výsledky pre štartovné číslo
        /// </summary>
        public List<ChipResultsDto> GetByStartNumber(int startNumber, int competitionId)
        {
            var startNumberParameter = new SqlParameter("@StartNumber", startNumber);
            var competitionIdParameter = new SqlParameter("@CompetitionId", competitionId);

            return this._unitOfWork.ExecuteQuery<ChipResultsDto>("EXEC GetChipDataByStartNumber @StartNumber, @CompetitionId", startNumberParameter, competitionIdParameter)
                    .ToList();
        }

        public List<ChipSplitsDto> GetSplits(DisciplineList disciplineList)
        {
            List<ChipSplitsDto> chipSplits = new List<ChipSplitsDto>();

            string sql = "EXEC GetSplitsForDisciplineListId @DisciplineListId, '2013-10-05 19:59:10'";
            SqlCommand sqlCommand = new SqlCommand(sql);
            sqlCommand.Parameters.AddWithValue("@DisciplineListId", disciplineList.ID); 
            DataTable dt = this._unitOfWork.ExecuteQueryToTable(sqlCommand);

            if (dt != null && dt.Rows.Count > 0 && dt.Columns.Count > 0)
            {
                foreach (DataRow dr in dt.Rows)
                {
                    foreach (DataColumn col in dt.Columns)
                    {
                        if (col.ColumnName != "ID" & !col.ColumnName.Contains("Order"))
                        {
                            ChipSplitsDto chipSplitDto = new ChipSplitsDto();
                            chipSplitDto.PerformanceId = Convert.ToInt32(dr["ID"]);
                            chipSplitDto.CheckPointId = Convert.ToInt32(col.ColumnName);
                            chipSplitDto.CheckPointName = disciplineList.CheckPoints.Single(cp => cp.ID == chipSplitDto.CheckPointId).Name;

                            chipSplitDto.Order = Convert.ToInt32(dr[col.ColumnName + "_Order"]);
                            chipSplitDto.ChipTimeSpan = Convert.ToDateTime(dr[col.ColumnName]).TimeOfDay;

                            chipSplits.Add(chipSplitDto);
                        }
                    }
                }
            }

            return chipSplits;
            
        }




    }
}
